﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyMusicPlay._4._0
{
    public class ReadMusic
    {
        private static string[] FormatArr = { "mp3", "mp4", "m4a", "flac", "wav" };
        /// <summary>
        /// 读取歌单路径
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public Dictionary<int, Music> ReadMusicList(string path, string listName)
        {
            Dictionary<int, Music> keyValuePairs = new();

            List<string> musicArr = null;

            try { musicArr = Directory.GetFiles(path).ToList(); }
            catch { MyMessageBox.Show("啊嘞!歌单目录好像出现了问题,重新载入一个吧!"); }

            if (musicArr == null || musicArr is { Count: 0 })
                return keyValuePairs;

            int index = 0;
            foreach (var item in musicArr)
            {
                var suffix = item.Substring(item.LastIndexOf('.') + 1);
                if (FormatArr.Any(u => u == suffix))
                {
                    var name = item.Substring(item.LastIndexOf('\\') + 1);
                    name = name.Substring(0, name.IndexOf('.'));

                    var arrAll = name.Split("-");
                    var songer = "";
                    if (arrAll.Length > 1)
                    {
                        name = arrAll[0].Trim();
                        songer = arrAll[1].Trim();
                    }

                    Music music = new(name, item, songer, listName, index);

                    keyValuePairs.Add(index, music);
                    index++;
                }
            }
            return keyValuePairs;
        }
    }
}
